package com.mhtool.core.io;

import lombok.experimental.UtilityClass;
import org.apache.commons.io.Charsets;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author yin
 * @date 2021/12/27 17:20
 * @since 8.0.0
 */
@UtilityClass
public class FileUtils extends org.apache.commons.io.FileUtils {

    static Charset UTF8 = Charsets.toCharset("utf-8");

    public static String readFileToString(String absolutePath) throws IOException {
        return readFileToString(new File(absolutePath));
    }

    public static String readFileToString(final File file) throws IOException {
        return org.apache.commons.io.FileUtils.readFileToString(file, UTF8);
    }

    public static List<String> readLines(String absolutePath) throws IOException {
        return readLines(new File(absolutePath), UTF8);
    }
    public static List<String> readLines(final File file) throws IOException {
        return org.apache.commons.io.FileUtils.readLines(file, UTF8);
    }

    public static void writeStringToFile(final String absolutePath, String data) {
        try {
            writeStringToFile(new File(absolutePath), data, UTF8);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void writeStringToFile(final File file, String data) throws IOException {
        org.apache.commons.io.FileUtils.writeStringToFile(file, data, UTF8);
    }
}